package com.ecoolex.ying.space.core.manager;

import java.sql.Date;
import java.time.Instant;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.ecoolex.framework.common.enums.ResultCode;
import com.ecoolex.framework.common.pojo.Page;
import com.ecoolex.framework.common.pojo.Pagination;
import com.ecoolex.framework.common.util.BizAssert;
import com.ecoolex.framework.common.util.Check;
import com.ecoolex.framework.common.util.CollectorUtil;
import com.ecoolex.ying.space.core.dao.WholeHouseCaseItemsDAO;
import com.ecoolex.ying.space.entity.WholeHouseCase;
import com.ecoolex.ying.space.entity.WholeHouseCaseItems;
import com.ecoolex.ying.space.entity.WholeHouseCaseItems.Column;
import com.ecoolex.ying.space.entity.WholeHouseCaseItemsExample;
import com.ecoolex.ying.space.entity.WholeHouseCaseItemsExample.Criteria;
import com.ecoolex.ying.space.pojo.AddWholeHouseCaseItemsDTO;

@Service
public class WholeHouseCaseItemsManager {
	
    @Autowired
    private WholeHouseCaseItemsDAO dao;
    
    public void updateWholeHouseCaseItems(WholeHouseCase record,List<AddWholeHouseCaseItemsDTO> itemsDTOList, String createUser) {
    	List<WholeHouseCaseItems> oldList = getByCaseId(record.getCaseId());
    	for (int i = 0;i<itemsDTOList.size();i++) {
    		AddWholeHouseCaseItemsDTO dto = itemsDTOList.get(i);
    		//修改
    		if(Check.notNull(dto.getItemId())) {
    			WholeHouseCaseItems old = oldList.stream().collect(CollectorUtil.first(o -> dto.getItemId().equals(o.getItemId())));
    			BizAssert.isTrue(Check.notNull(old), ResultCode.VERIFY.build(30),"案例栏目={}，不存在",dto.getItemId());
    			old.setName(dto.getName());
    			old.setDescription(dto.getDescription());
    			old.setOutsourcingPackagedId(dto.getOutsourcingPackagedId());
    			old.setOrdered(i+1);
    			JSONArray jsonArray = new JSONArray();
    			List<String> imageList = dto.getImages();
    			for (String storageId : imageList) {
					jsonArray.add(storageId);
				}
    			old.setImages(jsonArray.toString());
    			old.setUpdateUser(createUser);
    			old.setUpdateTime(Date.from(Instant.now()));
    			dao.updateByPrimaryKeySelective(old, Column.excludes(Column.deleted,Column.createUser,Column.createTime));
    			
    			oldList.remove(old);
    		}else {//编辑
    			WholeHouseCaseItems wholeHouseCaseItems = new WholeHouseCaseItems();
    			wholeHouseCaseItems.setName(dto.getName());
    			wholeHouseCaseItems.setOutsourcingPackagedId(dto.getOutsourcingPackagedId());
    			wholeHouseCaseItems.setDescription(dto.getDescription());
    			wholeHouseCaseItems.setOrdered(i+1);
    			wholeHouseCaseItems.setCaseId(record.getCaseId());
    			wholeHouseCaseItems.setDeleted(WholeHouseCaseItems.NOT_DELETED);
    			wholeHouseCaseItems.setCreateUser(createUser);
    			wholeHouseCaseItems.setCreateTime(Date.from(Instant.now()));
    			wholeHouseCaseItems.setUpdateUser(createUser);
    			wholeHouseCaseItems.setUpdateTime(Date.from(Instant.now()));
    			JSONArray jsonArray = new JSONArray();
    			List<String> imageList = dto.getImages();
    			for (String storageId : imageList) {
					jsonArray.add(storageId);
				}
    			wholeHouseCaseItems.setImages(jsonArray.toString());
    			dao.insert(wholeHouseCaseItems);
    		}
		}
    	oldList.forEach(old -> delete(old,createUser));
    }
    
    public void delete(WholeHouseCase record, String updateUser) {
    	List<WholeHouseCaseItems> list = getByCaseId(record.getCaseId());
    	for (WholeHouseCaseItems items : list) {
			items.setDeleted(WholeHouseCaseItems.IS_DELETED);
			items.setUpdateUser(updateUser);
			items.setUpdateTime(Date.from(Instant.now()));
			dao.updateByPrimaryKeySelective(items, Column.deleted,Column.updateTime,Column.updateUser);
		}
    }
    
    public void delete(WholeHouseCaseItems items, String updateUser) {
		items.setDeleted(WholeHouseCaseItems.IS_DELETED);
		items.setUpdateUser(updateUser);
		items.setUpdateTime(Date.from(Instant.now()));
		dao.updateByPrimaryKeySelective(items, Column.deleted,Column.updateTime,Column.updateUser);
    }
    
    public List<WholeHouseCaseItems> getByCaseId(Integer caseId){
    	WholeHouseCaseItemsExample example = new WholeHouseCaseItemsExample();
    	Criteria criteria = example.createCriteria();
    	criteria.andCaseIdEqualTo(caseId).andDeletedEqualTo(WholeHouseCaseItems.NOT_DELETED);
    	return dao.selectByExample(example);
    }
    
    public long countByItemsName(String itemsName) {
    	return dao.countByItemsName(itemsName);
    }
    
    public Pagination<String> queryStyleDescriptionByItemsName(String itemsName,Page page){
    	return Pagination.create(page,
    		dao.countStyleDescriptionByItemsName(itemsName),
    		() -> dao.queryStyleDescriptionByItemsName(itemsName, page));
    }
    
    public Pagination<WholeHouseCaseItems> queryByItemsNameAndStyleDescription(String itemsName,String styleDescription,Page page){
    	return Pagination.create(page,
    		dao.countByItemsNameAndStyleDescription(itemsName, styleDescription),
    		() -> dao.queryByItemsNameAndStyleDescription(itemsName, styleDescription, page));
    }
}